import pandas as pd
import tushare as ts
import numpy as np
from emmodels.em_base_data import BaseData
import datetime
from LSTM.variableset import EMVar,EMPath
from emutils import series_utils
from em_logger import logger as el
from emutils import stk_utils

def daily_stk_list(stk_list=None, to_csv=True):
    if isinstance(stk_list, list):
        for code in stk_list:
            predict_data_source(stkcode=code, to_csv=to_csv)



def predict_data_source(stkcode=EMVar.DATA_SOURCE, force_append_now=False, to_csv=True):
    try:
        end = datetime.datetime.now()
        end_time = end.strftime('%Y-%m-%d')
        df = ts.get_k_data(code=stkcode, index=False, start='2010-01-01', end=end_time, autype='qfq', ktype='D')
        # df = new_df[EMVar.BASE_COLUMNS]

        if stk_utils.need_append_now(df['date'].iloc[-1]) or force_append_now:
            today_s = stk_utils.nowday_h_data(stkcode)
            if not today_s.empty:
                df = df.append(today_s, ignore_index=True)
            else:
                el.debug('%s 在 %s 数据为空' % (stkcode, end_time))

        df[EMVar.label] = series_utils.data_offset(df[EMVar.close], offset=-1)
        df[EMVar.dm1_close] = series_utils.data_offset(df[EMVar.close], offset=1)
        df[EMVar.dm2_close] = series_utils.data_offset(df[EMVar.close], offset=2)
        df[EMVar.dm1_ajx_price] = (df[EMVar.close] - df[EMVar.dm1_close]) / df[EMVar.dm1_close]
        df[EMVar.dm2_ajx_price] = (df[EMVar.dm1_close] - df[EMVar.dm2_close]) / df[EMVar.dm1_close]

        INPUT_COLUMNS = EMVar.BASE_COLUMNS +[EMVar.dm1_close, EMVar.dm1_ajx_price,
                                             EMVar.dm2_close, EMVar.dm2_ajx_price]
        OUTPUT_COLUMNS = [EMVar.label]
        total_out_put_columns = INPUT_COLUMNS + OUTPUT_COLUMNS
        output_df = df[INPUT_COLUMNS+OUTPUT_COLUMNS]
        if to_csv:
            df.to_csv(path_or_buf=EMPath.data_file_full_path(code=stkcode, fileName=(stkcode + '.csv')), columns=INPUT_COLUMNS + OUTPUT_COLUMNS, index=False)
        EMVar.save_env(code=stkcode, params={'数据集': stkcode, '数据序列': total_out_put_columns})
        return {'data': output_df,
                'inputData': len(INPUT_COLUMNS),
                "outputData": len(OUTPUT_COLUMNS)}
    except Exception as e:
        print('%s 历史数据获取失败'%stkcode)
        print(e)
        return None




# daily_stk_data()

# daily_stk_list(EMVar.DATA_SOURCE_LIST, to_csv=False)
